package routefinder.renderer;

import java.util.logging.Logger;
import routefinder.Configuration;
import routefinder.exception.RouteFinderException;

/**
 * Implements a factory of {@link routefinder.renderer.IRenderer} objects.
 * Will load  the class specified in the application.properties.
 *
 * @author luky
 */
public class RendererFactory {

    /**
     * Creates a new instance of {@link routefinder.renderer.IRenderer}.
     *
     * @return
     */
    public static IRenderer newInstance(){
        IRenderer renderer = null;
        try {
            String rendererClass = Configuration.getInstance().getProperty("renderer.class");
            Logger.getLogger(RendererFactory.class.getName()).info("Renderer class: " + rendererClass);
            renderer = (IRenderer) Class.forName(rendererClass).newInstance();
        } catch (IllegalAccessException ex) { //TODO refactor
            throw new RouteFinderException("Unable to create the renderer", ex);
        } catch (ClassNotFoundException ex) {
            throw new RouteFinderException("Unable to create the renderer",  ex);
        } catch(InstantiationException iEx){
            throw new RouteFinderException("Unable to create the renderer", iEx);
        }

        return renderer;
    }
}
